import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
Vue.use(VueRouter)

const routes = [
  {
	path: '/login',
	name: 'Login',
	component: () => import('@/views/login')
  },
  {
	path: '/index',
	name: 'Index',
	component: () => import('@/views'),
	children: [
      {
        path: '/user',
        name: 'UserList',
        component: () => import('@/views/ums/user/list')
      },
      {
        path: '/role',
        name: 'RoleList',
        component: () => import('@/views/ums/role/list')
      },
      {
        path: '/resources',
        name: 'ResourceList',
        component: () => import('@/views/ums/resources/list')
      }
	]
  },
  {
	path:'*',
	name:'ErrorPage',
	component: () => import('@/views/error/ErrorPage')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const white = ['/login']
router.beforeEach((to,from,next) => {
  for(let i = 0 ; i < white.length; i++) {
	for(let j = 0; j < to.matched.length; j++) {
      if(to.matched[j].regex.test(white[i])) {
		next()
		return
      }
	}
  }
  const token = store.getters.GET_TOKEN
  if(token === '') {
	next('/login')
  } else {
	next()
  }
})

export default router
